package com.rxh.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rxh.mapper.core.CoreAcquirerMapper;
import com.rxh.mapper.core.CoreCargoTrackMapper;
import com.rxh.mapper.core.CoreOrderMapper;
import com.rxh.mapper.merchant.MerchantInfoMapper;
import com.rxh.mapper.merchant.MerchantWebSiteMapper;
import com.rxh.mapper.risk.*;
import com.rxh.mapper.sys.SysConstantMapper;
import com.rxh.pojo.base.Page;
import com.rxh.pojo.base.PageResult;
import com.rxh.pojo.base.SearchInfo;
import com.rxh.pojo.core.CoreCardholder;
import com.rxh.pojo.core.CoreCargoTrack;
import com.rxh.pojo.core.CoreOrder;
import com.rxh.pojo.core.CoreOrderDetail;
import com.rxh.pojo.risk.*;
import com.rxh.service.RiskService;
import com.rxh.utils.SystemConstant;
import com.rxh.utils.UUID;
import com.rxh.vo.VoCoreOrderExport;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class RiskServiceImpl implements RiskService {

    @Resource
    private RiskLimitMapper riskLimitMapper;

    @Resource
    private SysConstantMapper constantMapper;

    @Resource
    private MerchantInfoMapper merchantInfoMapper;

    @Resource
    private MerchantWebSiteMapper merchantWebSiteMapper;

    @Resource
    private CoreAcquirerMapper acquirerMapper;

    @Resource
    private RiskQuotaMapper riskQuotaMapper;

    @Resource
    private RiskRefuseMapper riskRefuseMapper;

    @Resource
    private RiskExtendMapper riskExtendMapper;

    @Resource
    private RiskDangerMapper riskDangerMapper;

    @Resource
    private RiskBusinessMapper riskBusinessMapper;

    @Resource
    private RiskIssuerMapper riskIssuerMapper;

    @Resource
    private CoreOrderMapper orderMapper;

    @Resource
    private CoreCargoTrackMapper coreCargoTrackMapper;

    @Resource
    private RiskQuotaDataMapper riskQuotaDataMapper;



    /*
     *  获取下拉框参数
     */

    public Map<String, Object> getInit() {
        Map<String, Object> mapParam = new HashMap<>();
        mapParam.put("merchant", merchantInfoMapper.selectAllIdAndName());      //商户信息
        mapParam.put("siteurl", merchantWebSiteMapper.selectAllIdAndMerIdAndWeb());     //域名
        mapParam.put("acquirer", acquirerMapper.selectAll());       //收单机构
        mapParam.put("limitConstant", constantMapper.selectByGroupName(SystemConstant.RISK_LIMIT));    //限制元素
        mapParam.put("stopConstant", constantMapper.selectByGroupName(SystemConstant.RISK_STOPS));     //拒绝元素
        mapParam.put("statusConstant", constantMapper.selectByGroupName(SystemConstant.RISK_STATUS));  //状态
        mapParam.put("country", constantMapper.selectByGroupName(SystemConstant.RISK_COUNTRY));  //国家
        mapParam.put("issuer", constantMapper.selectByGroupName(SystemConstant.RISK_ISSUER));      //发卡行
        mapParam.put("extrisk", constantMapper.selectByGroupName(SystemConstant.RISK_EXTRISK));    //风控类型
        mapParam.put("credit", constantMapper.selectByGroupName(SystemConstant.RISK_CREDIT)); //信用卡支付
        return mapParam;
    }

    // 风控限次管理表
    /*
     * 获取全部数据
     */
    @Cacheable(value = "risk_limit", key = "'risk_limit'")
    @Override
    public List<RiskLimit> getIndefinitely() {
        // Map<String, Object> resultMap = new HashMap<>();
        // Integer pageFirst = Integer.parseInt(paramMap.get("pageFirst") + "");
        // Integer pageSize = Integer.parseInt(paramMap.get("pageSize") + "");
        // pageFirst = pageFirst * pageSize;
        // paramMap.put("pageFirst", pageFirst);
        // paramMap.put("pageSize", pageSize);
        List<RiskLimit> list = riskLimitMapper.getIndefinitely();
        // int result = riskLimitMapper.getIndefinitelyCheckNum();
        // resultMap.put("result",result);
        // list.add(resultMap);
        return list;
    }


    /*
     *  添加数据
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_limit", key = "'risk_limit'", condition = "#result == 1")
    })
    @Override
    public Integer addRiskLimit(RiskLimit riskLimit) {
        riskLimit.setCreateTime(new Date());
        riskLimit.setId(UUID.createKey("risk_limit"));
        return riskLimitMapper.insertSelective(riskLimit);
    }

    /*
     *  判断是否能添加
     */
    public Integer addIndefinitely(RiskLimit riskLimit) {
        return riskLimitMapper.getindefinitelyAdd(riskLimit);
    }

    /*
     *  启用与禁用模块
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_limit", key = "'risk_limit'", condition = "#result == true ")
    })
    @Override
    public Boolean updateindefinitelyLocked(RiskLimit riskLimit) {
        return riskLimitMapper.updateByPrimaryKeySelective(riskLimit) == 1;
    }

    /*
     *  更新数据
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_limit", key = "'risk_limit'", condition = "#result == 1")
    })
    @Override
    public Integer updateRiskLimit(RiskLimit riskLimit) {
        riskLimit.setModifyTime(new Date());
        return riskLimitMapper.updateByPrimaryKeySelective(riskLimit);
    }

    /*
     *  删除数据
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_limit", key = "'risk_limit'", condition = "#result == 1")
    })
    @Override
    public Integer deleteRiskLimitById(long id) {
        return riskLimitMapper.deleteByPrimaryKey(id);
    }

    /*
     *  批量删除数据
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_limit", key = "'risk_limit'", condition = "#result > 0")
    })
    @Override
    public Integer deleteBatchRiskLimit(String[] idArr) {
        return riskLimitMapper.getindefinitelyBatchDel(idArr);
    }

    /*
     *  根据交易时间和id来获取bank_status
     *
     */
    public List<Map<String,Object>> getCountCoreOrder(CoreOrder coreOrder){
        return orderMapper.getCountCoreOrder(coreOrder);
    }

    public List<Map<String,Object>> getCountCoreCardHolder(CoreOrder coreOrder, CoreCardholder coreCardholder) {
        return orderMapper.getCountCoreCardHolder(coreOrder, coreCardholder);
    }

    public List<Map<String,Object>> getCountCardHolderId(CoreOrder coreOrder, CoreOrderDetail coreOrderDetail) {
        return orderMapper.getCountCardHolderId(coreOrder,coreOrderDetail);
    }


    //风控限额管理表

    /*
     *  获取全部数据
     */

    @Cacheable(value = "risk_quota", key = "'risk_quota'")
    @Override
    public List<RiskQuota> getLimit() {
        return riskQuotaMapper.getLimit();
    }

    /*
     *  启用与禁用模块
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_quota", key = "'risk_quota'", condition = "#result == true")
    })
    @Override
    public Boolean updateRiskQuotaLocked(RiskQuota riskQuota) {
        return riskQuotaMapper.updateByPrimaryKeySelective(riskQuota) == 1;
    }

    /*
     *  删除用户模块
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_quota", key = "'risk_quota'", condition = "#result == true")
    })
    @Override
    public Boolean deleteRiskQuotaByid(Long id) {
        return riskQuotaMapper.deleteByPrimaryKey(id) == 1;
    }


    /*
     *  新增用户模块
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_quota", key = "'risk_quota'", condition = "#result == true")
    })
    @Override
    public Boolean addRiskQuota(RiskQuota riskQuota) {
        riskQuota.setCreateTime(new Date());
        riskQuota.setId(UUID.createKey("risk_quota"));
        return riskQuotaMapper.insertSelective(riskQuota) == 1;
    }

    /*
     *  判断是否可以添加
     */
    public Integer insertRiskQuota(RiskQuota riskQuota) {
        return riskQuotaMapper.getlimitAddCount(riskQuota);
    }

    /*
     *  更新用户模块
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_quota", key = "'risk_quota'", condition = "#result == true")
    })
    @Override
    public Boolean updateRiskQuota(RiskQuota riskQuota) {
        riskQuota.setModifyTime(new Date());
        return riskQuotaMapper.updateByPrimaryKeySelective(riskQuota) == 1;
    }

    /*
     *   批量删除
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_quota", key = "'risk_quota'", condition = "#result > 0")
    })
    @Override
    public Integer deleteBatchRiskQuota(String[] idArr) {
        return riskQuotaMapper.getlimitBatchDel(idArr);
    }

    /**
     *  Risk_Quota_Data
     * @return
     */
    //获取全部数据
    // @Cacheable(value = "risk_quota_data",key = "'risk_quota_data'")
    public List<RiskQuotaData> getQuotaData() {
        return  riskQuotaDataMapper.getQuotaData();
    }

    @Override
    public Integer insertOrUpdateRiskQuotaData(List<RiskQuotaData> quotaDataList) {
        return riskQuotaDataMapper.insertOrUpdateRiskQuotaData(quotaDataList);
    }

    //风控拒绝管理表

    /*
     *  获取全部数据
     */
    @Cacheable(value = "risk_refuse", key = "'risk_refuse'")
    public List<RiskRefuse> getRefuse() {
        return riskRefuseMapper.getRefuse();
    }

    /*
     *  启用与禁用模块
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_refuse", key = "'risk_refuse'", condition = "#result == true")
    })
    public Boolean updaterefusedLocked(RiskRefuse riskRefuse) {
        return riskRefuseMapper.updateByPrimaryKeySelective(riskRefuse) == 1;
    }

    /*
     *  删除用户模块
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_refuse", key = "'risk_refuse'", condition = "#result == true")
    })
    public Boolean deleteRiskRefuse(Long id) {
        return riskRefuseMapper.deleteByPrimaryKey(id) == 1;
    }

    /*
     *  新增用户模块
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_refuse", key = "'risk_refuse'", condition = "#result == true")
    })
    public Boolean addRiskRefuse(RiskRefuse riskRefuse) {
        riskRefuse.setCreateTime(new Date());
        riskRefuse.setId(UUID.createKey("risk_refuse"));
        return riskRefuseMapper.insertSelective(riskRefuse) == 1;
    }

    /*
     *  判断是否可添加
     */
    public Integer insertRiskRefuse(RiskRefuse riskRefuse) {
        return riskRefuseMapper.getrefusedAddCount(riskRefuse);
    }

    /*
     *  更新用户模块
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_refuse", key = "'risk_refuse'", condition = "#result == true")
    })
    @Override
    public Boolean updateRiskRefuse(RiskRefuse riskRefuse) {
        riskRefuse.setModifyTime(new Date());
        return riskRefuseMapper.updateByPrimaryKeySelective(riskRefuse) == 1;
    }

    /*
     *  批量删除
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_refuse", key = "'risk_refuse'", condition = "#result > 0")
    })
    public Integer deleteBatchRiskRefuse(String[] idArr) {
        return riskRefuseMapper.getrefusedBatchDel(idArr);
    }

    //外置风控管理表
    /*
     *  获取全部数据
     */

    @Cacheable(value = "risk_extend", key = "'risk_extend'")
    @Override
    public List<RiskExtend> getOuter() {
        return riskExtendMapper.getOuter();
    }

    /*
     * 启用与禁用模块
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_extend", key = "'risk_extend'", condition = "#result == true")
    })
    @Override
    public Boolean updateouterLocked(RiskExtend riskExtend) {
        return riskExtendMapper.updateByPrimaryKeySelective(riskExtend) == 1;
    }

    /*
     * 删除外置风控
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_extend", key = "'risk_extend'", condition = "#result == true")
    })
    @Override
    public Boolean deleteRiskExtend(Long id) {

        return riskExtendMapper.deleteByPrimaryKey(id) == 1;
    }

    /*
     * 新增外置风控
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_extend", key = "'risk_extend'", condition = "#result == true")
    })
    @Override
    public Boolean addRiskExtend(RiskExtend riskExtend) {
        riskExtend.setCreateTime(new Date());
        riskExtend.setId(UUID.createKey("risk_extend"));
        return riskExtendMapper.insertSelective(riskExtend) == 1;
    }

    /*
     *  判断用户是否可以添加
     */
    public Integer insertRiskExtend(RiskExtend riskExtend) {
        return riskExtendMapper.getouterAddCount(riskExtend);
    }

    /*
     * 更新外置风控
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_extend", key = "'risk_extend'", condition = "#result == true")
    })
    @Override
    public Boolean updateRiskExtend(RiskExtend riskExtend) {
        riskExtend.setModifyTime(new Date());
        return riskExtendMapper.updateByPrimaryKeySelective(riskExtend) == 1;
    }

    /*
     *  批量操作
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_extend", key = "'risk_extend'", condition = "#result > 0")
    })
    @Override
    public Integer deleteBatchRiskExtend(String[] idArr) {
        return riskExtendMapper.getouterBatchDel(idArr);
    }

    //黑卡管理表

    /**
     * 获取全部数据
     */
    /*@Caching(evict = {
            @CacheEvict(value = "risk_danger", key = "'risk_danger'")
    })*/
    @Cacheable(value = "risk_danger", key = "'risk_danger'")
    @Override
    public List<RiskDanger> getBlackcard() {
        return riskDangerMapper.getBlackcard();
    }

    /**
     * 启用与禁用模块
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_danger", key = "'risk_danger'", condition = "#result == true")
    })
    @Override
    public Boolean updateblackcardLocked(RiskDanger riskDanger) {
        return riskDangerMapper.updateByPrimaryKeySelective(riskDanger) == 1;
    }

    /*
     *  删除用户模块
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_danger", key = "'risk_danger'", condition = "#result == true")
    })
    @Override
    public Boolean deleteRiskDanger(Long id) {
        return riskDangerMapper.deleteByPrimaryKey(id) == 1;
    }

    /**
     * 新增用户模块
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_danger", key = "'risk_danger'", condition = "#result == true")
    })
    @Override
    public Boolean addRiskDanger(RiskDanger riskDanger) {
        riskDanger.setCreateTime(new Date());
        riskDanger.setId(UUID.createKey("risk_danger"));
        return riskDangerMapper.insertSelective(riskDanger) == 1;
    }

    /**
     * 判断是否可以添加
     *
     * @param riskDanger
     * @return
     */
    public Integer insertRiskDanger(RiskDanger riskDanger) {
        return riskDangerMapper.getblackcardAddCount(riskDanger);
    }

    /**
     * 更新用户模块
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_danger", key = "'risk_danger'", condition = "#result == true")
    })
    @Override
    public Boolean updateRiskDanger(RiskDanger riskDanger) {
        riskDanger.setModifyTime(new Date());
        return riskDangerMapper.updateByPrimaryKeySelective(riskDanger) == 1;
    }

    /**
     * 批量删除模块
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_danger", key = "'all'", condition = "#result > 0")
    })
    @Override
    public Integer deleteBatchRiskDanger(String[] idArr) {
        return riskDangerMapper.getblackcardBatchDel(idArr);
    }
    //商务卡管理表

    /**
     * 获取商务卡表的全部数据
     */
    @Cacheable(value = "risk_business", key = "'risk_business'")
    @Override
    public List<RiskBusiness> getBusiness() {
        return riskBusinessMapper.getBusiness();
    }

    /**
     * 新增用户模块
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_business", key = "'risk_business'", condition = "#result == true")
    })
    @Override
    public Boolean addRiskBusiness(RiskBusiness riskBusiness) {
        riskBusiness.setCreateTime(new Date());
        riskBusiness.setId(UUID.createKey("risk_business"));
        return riskBusinessMapper.insertSelective(riskBusiness) == 1;
    }

    /**
     * 判断用户是否可以添加
     *
     * @param riskBusiness
     * @return
     */
    @Override
    public Integer insertRiskBusiness(RiskBusiness riskBusiness) {
        return riskBusinessMapper.getbusinessAddCount(riskBusiness);
    }

    /**
     * 更新模块
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_business", key = "'risk_business'", condition = "#result == true")
    })
    @Override
    public Boolean updateRiskBusiness(RiskBusiness riskBusiness) {
        riskBusiness.setModifyTime(new Date());
        return riskBusinessMapper.updateByPrimaryKeySelective(riskBusiness) == 1;
    }

    /**
     * 批量删除模块
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_business", key = "'risk_business'", condition = "#result > 0")
    })
    @Override
    public Integer deleteBatchRiskBusiness(String[] idArr) {
        return riskBusinessMapper.getbusinessBatchDel(idArr);
    }

    //发卡行管理表

    /**
     * 获取发卡行表的全部数据
     */
    @Cacheable(value = "risk_issuer", key = "'risk_issuer'")
    @Override
    public List<RiskIssuer> getIssuer() {
        return riskIssuerMapper.getIssuer();
    }

    /**
     * 新增操作
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_issuer", key = "'risk_issuer'", condition = "#result == true")
    })
    @Override
    public Boolean addRiskIssuer(RiskIssuer riskIssuer) {
        riskIssuer.setCreateTime(new Date());
        riskIssuer.setId(UUID.createKey("risk_issuer"));
        return riskIssuerMapper.insertSelective(riskIssuer) == 1;
    }

    /**
     * 判断用户是否添加
     *
     * @param riskIssuer
     * @return
     */
    @Override
    public Integer insertRiskIssuer(RiskIssuer riskIssuer) {
        return riskIssuerMapper.getissuerAddCount(riskIssuer);
    }

    /**
     * 更新操作
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_issuer", key = "'risk_issuer'", condition = "#result == true")
    })
    @Override
    public Boolean updateRiskIssuer(RiskIssuer riskIssuer) {
        riskIssuer.setModifyTime(new Date());
        return riskIssuerMapper.updateByPrimaryKeySelective(riskIssuer) == 1;
    }

    /**
     * 批量删除
     */
    @Caching(evict = {
            @CacheEvict(value = "risk_issuer", key = "'risk_issuer'", condition = "#result > 0")
    })
    @Override
    public Integer deleteBatchRiskIssuer(String[] idArr) {
        return riskIssuerMapper.getissuerBatchDel(idArr);
    }

    /**
     * 物流管理初始条件查询
     *
     * @return
     */

    @Override
    public Map<String, Object> getLogisticsMgmtInit(Integer merchantId) {

        Map<String, Object> result = new HashMap<>();
        result.put("express", constantMapper.selectByGroupNameForMapKeyFirstValue(SystemConstant.EXPRESS));
        result.put("shipStatus", constantMapper.selectByGroupNameForMapKeyFirstValue(SystemConstant.SHIP_STATUS));
        result.put("siteUrl", merchantWebSiteMapper.selectByMerchantId(merchantId));
        return result;
    }

    /**
     * 物流管理表格
     *
     * @return
     */
    @Override
    public PageResult getLogisticsMgmtInfo(Page page) {

        // 设置分页信息
        PageHelper.startPage(page.getPageNum(), page.getPageSize());

        if (null != page.getSearchInfo().getIdSearchType() && page.getSearchInfo().getIdSearchType() == 1) {
            page.getSearchInfo().setMerOrderId(page.getSearchInfo().getId());
            page.getSearchInfo().setId(null);
        }
        // 执行查询
        List<Map<String, Object>> list = orderMapper.selectByLogisticsMgmtInfo(page.getSearchInfo());
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        // 获取查询结果
        PageResult result = new PageResult();
        result.setRows(list);
        result.setTotal(pageInfo.getTotal());
        result.setAllPage(pageInfo.getPages());
        // 返回结果
        return result;


    }

    /**
     * 物流管理 上传快递单号
     *
     * @return
     */

    @Override
    public int uploadExpressInfo(CoreCargoTrack coreCargoTrack) {

        CoreOrder coreOrder = new CoreOrder();
        coreOrder.setId(coreCargoTrack.getOrderId());
        coreOrder.setShipStatus(SystemConstant.SHIPPED);
        return coreCargoTrackMapper.insertSelective(coreCargoTrack) == 1 ? orderMapper.updateByPrimaryKeySelective(coreOrder) : 0;

    }

    @Override
    public List<VoCoreOrderExport> getExportOrderInfo(SearchInfo searchInfo) {

        if (null != searchInfo.getIdSearchType() && searchInfo.getIdSearchType() == 1) {
            searchInfo.setMerOrderId(searchInfo.getId());
            searchInfo.setId(null);
        }

        return orderMapper.getExportLogisticsMgmtInfo(searchInfo);
    }


    /**
     * 物流管理 批量更新快递信息
     *
     * @return
     */

    @Override
    public int batchUpdateOrders(List<Map<String, Object>> recodes) {

        return coreCargoTrackMapper.batchInsertCargoTrack(recodes) == recodes.size() ? orderMapper.batchUpdateOrdersShipstatus(recodes) : 0;

    }

}
